#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;


namespace Amarok.Presentation
{
	/// <summary>
	/// </summary>
	internal abstract class HandlerInspector
	{
		#region ++ Public Interface ++

		/// <summary>
		/// </summary>
		/// <param name="methodInfo"></param>
		/// <returns></returns>
		public abstract HandlerInfo Inspect(MethodInfo methodInfo);

		#endregion

		#region ## Protected Interface ##

		protected void ReportInvalidHandler(MethodInfo methodInfo, String message)
		{
			#region (tracing)
			{
				Trace.WriteLine(String.Format(CultureInfo.InvariantCulture,
					"*** WARNING: Method '{0}' on type '{1}' is marked as view action handler, but does not comply " +
					"with the requirements for view action handlers: {2}",
					methodInfo.Name,
					methodInfo.ReflectedType.FullName,
					message));
			}
			#endregion
		}

		protected Boolean VerifyNotGeneric(MethodInfo methodInfo)
		{
			if (methodInfo.IsGenericMethod == false && methodInfo.IsGenericMethodDefinition == false)
				return true;

			ReportInvalidHandler(methodInfo,
				"Methods with generic type parameters are not allowed.");

			return false;
		}

		protected Boolean VerifyParameterCount(MethodInfo methodInfo, ParameterInfo[] parameters)
		{
			if (parameters.Length == 1)
				return true;

			ReportInvalidHandler(methodInfo,
				"Methods with less or more than one parameter are not allowed.");

			return false;
		}

		protected Boolean VerifyIsViewActionTypeParameter(MethodInfo methodInfo, ParameterInfo parameter)
		{
			if (typeof(ViewAction).IsAssignableFrom(parameter.ParameterType))
				return true;

			ReportInvalidHandler(methodInfo,
				"The method parameter type is not derived from 'Amarok.Presentation.ViewAction'.");

			return false;
		}

		protected Boolean VerifyBooleanReturnType(MethodInfo methodInfo)
		{
			if (methodInfo.ReturnType == typeof(Boolean))
				return true;

			ReportInvalidHandler(methodInfo,
				"The method does not have a return type of 'System.Boolean'.");

			return false;
		}

		#endregion

	}
}
